﻿using FrameWork.Core.Infrastructure;
using FrameWork.Data.Interface;
using FrameWork.Data.Mapping;
using FrameWork.Services;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Data;

namespace AncheStart.Converter
{
    public class TemplateIdToTemplateNameConvert : IValueConverter
    {
        public TemplateIdToTemplateNameConvert()
        {
        }

        private async void LoadProject()
        {
            if (GlobalConstant.Projects==null||GlobalConstant.Projects.Count == 0)
            {
                GlobalConstant.Projects=new List<ProjectInfo> ();
                var projectInfoManager = EngineContext.Current.Resolve<IProjectInfoManager>();
                //全部的项目
                GlobalConstant.Projects = await projectInfoManager.getAll();
            }
            if (GlobalConstant.StepTemplates==null||GlobalConstant.StepTemplates.Count == 0)
            {
                GlobalConstant.StepTemplates = new List<StepTemplate>();
                var stepTemplateManager = EngineContext.Current.Resolve<IStepTemplateManager>();
                foreach (var itemProject in GlobalConstant.Projects)
                {
                    //根据方案 读取模板信息
                    List<StepTemplate> listStepTemplate = stepTemplateManager.queryByProject(itemProject.Id);
                    //全部工步模板
                    listStepTemplate.ForEach(item =>
                    {
                        if (!GlobalConstant.StepTemplates.Any(s => s.Id == item.Id))
                        {
                            GlobalConstant.StepTemplates.Add(item);
                        }
                    });
                }
            }
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            LoadProject();
            if (GlobalConstant.Projects != null && GlobalConstant.StepTemplates.Any(s => s.Id == value.ObjToInt()))
            {
                return $"{value}-"+GlobalConstant.StepTemplates.First(s => s.Id == value.ObjToInt()).Title;
            }
            else
            {
                return $"{value}-无";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}